package cn.edu.zjicm.wordsnet_d.synchronize;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;

public class ExpInterface extends ServerInterface {

	private static ExpInterface mInstance;

	public static ExpInterface getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new ExpInterface(context);
		}
		return mInstance;
	}

	private ExpInterface(Context context) {
		super(context);
	}

	public void setExp() {
		Handler handler = new SetExpHandler();
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new setExpThread(handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		// MobclickAgentData.changeNickName(context);
	}

	public class setExpThread extends Thread {
		String token;
		int exp;
		Handler handler;

		setExpThread(Handler handler) {
			this.exp = AppPreference.getEXP(context);
			this.token = AppPreference.getToken(context);
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("exp", exp + "");
			NameValuePair pair2 = new BasicNameValuePair("t", token);
			list.add(pair1);
			list.add(pair2);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
				HttpPost post = new HttpPost(SET_EXP);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_UNSUCCESS));
					}
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
			}
		}
	};

	class SetExpHandler extends Handler {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case UserInterface.MSG_START:
				break;
			case UserInterface.MSG_SUCCESS:
				AppPreference.setEXPUpdated(context, true);
				LogUtil.e("设置经验值 成功");
				break;
			case UserInterface.MSG_UNSUCCESS:
				LogUtil.e("设置经验值 失败");
				break;
			case UserInterface.MSG_BAD_NETWORK:
				LogUtil.e("设置经验值 失败");
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				LogUtil.e("设置经验值 失败");
				break;
			default:
				break;
			}
		};
	};

	public void getExp() {
		Handler handler = new GetExpHandler();
		if (JudgeNetwork.getInstance(context).isWifiConnected() || JudgeNetwork.getInstance(context).isMobileConnected()) {
			new getExpThread(handler).start();
		} else {
			// Toast.makeText(context, "请确认网络是否连接", Toast.LENGTH_LONG).show();
			handler.sendMessage(handler.obtainMessage(UserInterface.MSG_BAD_NETWORK));
		}
		// MobclickAgentData.changeNickName(context);
	}

	public class getExpThread extends Thread {
		String token;
		Handler handler;

		getExpThread(Handler handler) {
			this.token = AppPreference.getToken(context);
			this.handler = handler;
		}

		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			setClientTimeout(client);

			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("t", token);
			list.add(pair1);
			try {
				HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
				HttpPost post = new HttpPost(GET_EXP);
				post.setEntity(entity);
				HttpResponse response = client.execute(post);

				if (response.getStatusLine().getStatusCode() == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					String message = result.getString("message");
					boolean success = result.getBoolean("success");
					if (success) {
						int exp = result.getInt("exp");
						AppPreference.setEXP(context, exp);
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_SUCCESS));
					} else {
						handler.sendMessage(handler.obtainMessage(UserInterface.MSG_UNSUCCESS));
					}
				} else {
					handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
				}
			} catch (Exception e) {
				e.printStackTrace();
				handler.sendMessage(handler.obtainMessage(UserInterface.MSG_WEB_SITE_ACCESS_FAILURE));
			}
		}
	};

	class GetExpHandler extends Handler {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case UserInterface.MSG_START:
				break;
			case UserInterface.MSG_SUCCESS:
				LogUtil.e("获取经验值 成功");
				break;
			case UserInterface.MSG_UNSUCCESS:
				LogUtil.e("获取经验值 失败");
				break;
			case UserInterface.MSG_BAD_NETWORK:
				LogUtil.e("获取经验值 失败");
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				LogUtil.e("获取经验值 失败");
				break;
			default:
				break;
			}
		};
	};

}
